package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.CityRegionDAO;
import model.EventDAO;

import com.google.gson.Gson;

/**
 * Servlet implementation class AmbitTypeServlet
 */
@WebServlet("/AmbitTypeServlet")
public class AmbitTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AmbitTypeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ambit = request.getParameter("ambit");
		ArrayList<String> results = new ArrayList<String>();
		results = EventDAO.getTypes(ambit);
		Gson gson = new Gson();
		String[] resultsArray = new String[results.size()];
		for (int i = 0; i < results.size(); i++) {
			resultsArray[i] = results.get(i);
		}
		System.out.println(gson.toJson("provaprova"));
		System.out.println(gson.toJson(resultsArray));
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
        //response.getWriter().write(new Gson().toJson("prova"));
		response.getWriter().write(new Gson().toJson(resultsArray));	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
